package com.edu.hre.struts.action.pim;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.edu.hre.common.entity.HrePageInfo;
import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.DynamicConfig;
import com.edu.hre.common.utils.RequestUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreUser;
import com.edu.hre.db.pojo.HreUserRoleScreen;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.sql.pim.EmpSkillDAOService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.pim.EmpSkillForm;
import com.edu.hre.struts.form.pim.EmpSkillListForm;
import com.google.gwt.http.client.Response;

public class EmpSkillListAction extends DispatchAction {

	private static final Logger logger = Logger.getLogger(EmpSkillListAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private static final EmpSkillDAOService empSkillDAOService = BeanFinder.getBean(EmpSkillDAOService.BEAN_NAME, EmpSkillDAOService.class);
	private final String ACTION_NAME = "empSkillList";
 
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {

		// Print source code location.
		CommonUtils.println(CommonUtils.getSourceCodeLocation());

		// Get the authorization of the current user for the specified operations. 
		HreUserRoleScreen userRoleScreen = (HreUserRoleScreen) request.getSession().getAttribute(RequestUtils.Attribute.HreUserRoleScreen);
		
		EmpSkillListForm beanForm = (EmpSkillListForm) form;
		beanForm.setUpdatable(userRoleScreen.isCanUpdate());

		String idStr = request.getParameter("id");

		HreUser tmpUser = (HreUser) request.getSession().getAttribute(RequestUtils.Attribute.USER_INFO);
		if (!EmployeeListAction.isAllowedToViewEmployeeList(tmpUser, CommonUtils.parseInt(idStr, -1))) {
			response.sendError(Response.SC_BAD_REQUEST);
			return null;
		}

		if (null != idStr) {
			int empNum = Integer.parseInt(idStr);
			beanForm.setEmpNumber(empNum);

			EmployeeListAction.setAttributesForRequest(beanForm.getEmpNumber(), request);
		}

		// Check if the operation is DELETE and the current user is authorized to DELETE.
		if (request.getParameter(StringConstants.BTN_DELETE) != null) {
			
			empSkillDAOService.deleteSkills(beanForm);
			EmployeeListAction.setAttributesForRequest(beanForm.getEmpNumber(), request);
		}

		// Set authorization information for the form in order to determine if the ADD or DELETE button is shown.
		beanForm.setCanAdd(userRoleScreen.isCanCreate());
		beanForm.setCanDelete(userRoleScreen.isCanDelete());
		
		// Get the sortBy parameter to determine how the result list is sorted! Then get the list and return.
		String sortBy = RequestUtils.getParameterStr(request, "sort", null);
		if (sortBy != null) { 
			beanForm.setAsc(!beanForm.isAsc());
			CommonUtils.sortCollection(beanForm.getAllSkills(), beanForm.isAsc(), sortBy);
			EmployeeListAction.setAttributesForRequest(beanForm.getEmpNumber(), request);
			return mapping.findForward(ACTION_NAME);
		}

		// Paging the displaying list.
		int currentPage = RequestUtils.getParameterInt(request, "page", 1);
		int pageSize = CommonUtils.parseInt(DynamicConfig.getConfig(DynamicConfig.PAGE_SIZE), 20);
		ArrayList<EmpSkillForm> allSkills = empSkillDAOService.getEmpSkillList(pageSize, pageSize * (currentPage - 1), beanForm.getEmpNumber());
		int countSkills = empSkillDAOService.countEmpSkills(beanForm.getEmpNumber());

		beanForm.setTotalPage(CommonUtils.getTotalPage(countSkills));
		beanForm.setAllSkills(allSkills);

		ArrayList<HrePageInfo> allPages = CommonUtils.getDisplayingPages(currentPage, CommonUtils.getTotalPage(countSkills));
		beanForm.setListPages(allPages);
		beanForm.setTotalItems(countSkills);

		EmployeeListAction.setAttributesForRequest(beanForm.getEmpNumber(), request);
		return mapping.findForward(ACTION_NAME);
	}
}
